import { getPermissionResourceApi } from "../../api/permission"
import { SET_MENUS, SET_PERMISSIONS } from "../mutation-types"

export default {
    state : {
        permissions : []
    },

    mutations : {
        [SET_PERMISSIONS](state, permissions) {
            state.permissions = permissions
        }
    },

    getters : {
        hasPermssion : (state) => (permission) => {
            return state.permissions.includes(permission)
        }
    },

    actions : {
        async getPermissionResource({state, commit, dispatch }) { //获取权限
            const resource = await getPermissionResourceApi()
            if(resource) {
                commit(SET_PERMISSIONS, resource.rules)
                //获取权限之后，获取菜单
                await dispatch('getMenus', resource.menus)
            }
            
        }
    }
}